/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pong;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.Timer;
import javax.swing.JFrame;

/**
 *
 * @author Administrator
 */
//TODO display Pong Game in JPanel
class MockPongModel implements PongModelInterface
{

    @Override
    public void movePaddle(VerticalDirection dir, Player player) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean update() {
        System.out.println("update" + ballLocation);
        ballLocation.translate(2, 2);
        return true;
    }

    @Override
    public int getScore(Player player) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Rectangle getPongCourt() {
        return new Rectangle(600,400);
    }

    private Point ballLocation =  new Point(100,200);
    @Override
    public Point getBallLocation() {
        //ballLocation.t
       return ballLocation;
    }

    @Override
    public int getBallSize() {
        return 15;
    }

    @Override
    public int getPaddleHeight(Player player) {
        return 50;
    }

    @Override
    public int getPaddleWidth(Player player) {
        return 10;
    }

    @Override
    public Point getPaddleLocation(Player player) {
        Point p1 = new Point(10,100);
        Point p2 = new Point(580,100);
        
        return player == Player.ONE ?  p1: p2; 
    }
}
/**
 *
 * @author Administrator
 */
//TODO display Pong Game in JPanel
public class PongFrame extends JFrame implements ActionListener, PongDataHandler
{
    PongModelInterface model;
    public PongFrame(PongModelInterface model)
    {
        this.model = model;
        
        this.model = new MockPongModel();
        // timer that updates game and repaint
        Timer timer = new Timer(100, this );
        timer.start();
        setSize(model.getPongCourt().width,model.getPongCourt().height); // use model size + chrome
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
    public void paint(Graphics g)
    {
        super.paint(g);
        
        // draw court
        Rectangle court = model.getPongCourt();
        g.setColor(Color.BLACK);
        g.drawRect(court.x,court.y,court.width,court.height);
        
        // draw ball
        g.fillOval(model.getBallLocation().x, model.getBallLocation().y, model.getBallSize(),model.getBallSize());    
        
        // draw paddles
        g.fillRect(model.getPaddleLocation(Player.ONE).x, model.getPaddleLocation(Player.ONE).y, 
                   model.getPaddleWidth(Player.ONE), model.getPaddleHeight(Player.ONE));
        g.fillRect(model.getPaddleLocation(Player.TWO).x, model.getPaddleLocation(Player.TWO).y, 
                   model.getPaddleWidth(Player.TWO), model.getPaddleHeight(Player.TWO));
                
    }
    
    // keyhandler
    
    @Override
    public void actionPerformed(ActionEvent e) {       
        model.update();
        repaint();
    }

    @Override
    public void receivePaddle(Direction d) {
        
    }

    @Override
    public void receiveBall(Ball b) {
        
    }
    Comm comm =  new NetworkedComm();
    class PaddleListener extends KeyAdapter
    {
        
        VerticalDirection networkDirection = new MockComm().networkDirection();
        
        @Override
        public void keyPressed(KeyEvent e)
        {
            switch(e.getKeyCode())
            {
                case KeyEvent.VK_UP:
                    model.movePaddle(VerticalDirection.UP, Player.ONE);
                    comm.sendPaddle(VerticalDirection.UP);
                    break;
                case KeyEvent.VK_DOWN:
                    model.movePaddle(VerticalDirection.DOWN, Player.ONE);
                    comm.sendPaddle(VerticalDirection.DOWN);
                    break;
            }
            
            switch(networkDirection)
            {
                case UP:
                    model.movePaddle(VerticalDirection.UP, Player.TWO);
                    break;
                case DOWN:
                    model.movePaddle(VerticalDirection.DOWN, Player.TWO);
                    break;
            }
            
        }
        
        // check for ball location conflicts (prefer network location)
        
    }
    
    class MockComm
    {
        public Player networkedPlayer()
        {
            return Player.TWO;
        }
        
        public VerticalDirection networkDirection()
        {
            return VerticalDirection.UP;
        }
        
        public Point networkBallLocation()
        {
            throw new UnsupportedOperationException("Not supported yet.");
        }
   }
    
}
